<template>
	<el-container>
		<el-aside style="float:left;width: 300px;">
			<el-card class="box-card el-card-flex">
				<div slot="header" class="clearfix">
					<el-input v-model="orgNameSearch" placeholder="请搜索..." suffix-icon="el-icon-search" clearable></el-input>
				</div>
				<div class="text item el-over-auto">
					<el-tree v-show="orgNameSearch" default-expand-all :props="orgTreeProps" :data="orgSearchDatas" @node-click="orgTreeNodeClick"
					 :ref="orgNameSearch?'orgTree':''" :highlight-current="true" node-key="orgId"></el-tree>
					<el-tree :default-expanded-keys="treeExpandedKey" v-show="!orgNameSearch" :props="orgTreeProps" :load="loadOrgDatas"
					 lazy @node-click="orgTreeNodeClick" :ref="!orgNameSearch?'orgTree':''" :highlight-current="true" node-key="orgId"
					 :key="treeKey"></el-tree>
				</div>
			</el-card>
		</el-aside>
		<el-main style="padding:0 0 0 10px;">
			<el-card class="box-card el-card-flex">
				<div slot="header" class="clearfix">
					<span>
						<i class="el-icon-warning-outline"></i>
						机构信息
					</span>
					<!-- <el-button plain type="danger" style="float: right;" class="el-button-pad" @click="orgDelEvent">
            删除
            <i class="el-icon-delete el-icon--right"></i>
          </el-button> -->
					<el-button plain type="info" style="float: right;margin-left:10px;" class="el-button-pad" @click="orgEditCancel"
					 v-show="enableEditFlag">
						取消
						<i class="el-icon-refresh-left el-icon--right"></i>
					</el-button>
					<el-button plain type="primary" style="float: right;margin-left:10px;" class="el-button-pad" @click="orgEditEvt"
					 v-show="enableEditFlag">
						保存
						<i class="el-icon-document-checked el-icon--right"></i>
					</el-button>
					<el-button plain type="primary" style="float: right;margin-left:10px;" class="el-button-pad" @click="enableEditEvt"
					 v-show="!enableEditFlag">
						编辑
						<i class="el-icon-edit el-icon--right"></i>
					</el-button>
					<el-button plain type="primary" style="float: right;" class="el-button-pad" @click="orgDialogShow">
						新增
						<i class="el-icon-plus el-icon--right"></i>
					</el-button>
				</div>
				<div class="text item el-over-auto" style="padding:10px 50px">
					<el-form v-show="enableEditFlag" :model="orgViewForm" ref="orgViewForm" label-width="100px">
						<el-form-item label="机构名称" prop="orgName" :rules="[
								{
									required: true,
									message: '机构名称不能为空'
								}
							]">
							<el-col :span="12">
								<el-input placeholder="请输入机构名称" v-model="orgViewForm.orgName"></el-input>
							</el-col>
						</el-form-item>
						<el-form-item label="机构代码" prop="orgCode">
							<el-col :span="9">
								<el-input placeholder="请输入机构代码" v-model="orgViewForm.orgCode" readonly></el-input>
							</el-col>
						</el-form-item>
						<el-form-item label="隶属机构">
							<el-col :span="18">
								<el-input readonly v-model="orgViewForm.parentOrgName"></el-input>
							</el-col>
						</el-form-item>
						<el-form-item label="机构类型">
							<el-radio-group v-model="orgViewForm.orgType">
								<el-radio v-for="item in orgTypeItems" :label="item.id" :key="item.id" border>{{ item.name }}</el-radio>
							</el-radio-group>
						</el-form-item>
						<el-form-item label="机构等级">
							<el-radio-group v-model="orgViewForm.orgDegree">
								<el-radio v-for="item in orgDegreeItems" :label="item.id" :key="item.id" border>{{ item.name }}</el-radio>
							</el-radio-group>
						</el-form-item>
						<el-form-item label="排序">
							<el-col :span="5">
								<el-input-number :precision="0" :min="1" v-model="orgViewForm.sortNo"></el-input-number>
							</el-col>
						</el-form-item>
						<el-form-item label="机构地址">
							<el-col :span="15">
								<el-input placeholder="请输入机构地址" v-model="orgViewForm.address"></el-input>
							</el-col>
						</el-form-item>
						<el-form-item label="邮编">
							<el-col :span="8">
								<el-input placeholder="请输入邮编" v-model="orgViewForm.zipCode"></el-input>
							</el-col>
						</el-form-item>
						<el-form-item label="联系人">
							<el-col :span="6">
								<el-input placeholder="请输入联系人" v-model="orgViewForm.linkMan"></el-input>
							</el-col>
						</el-form-item>
						<el-form-item label="联系电话">
							<el-col :span="9">
								<el-input placeholder="请输入联系电话" v-model="orgViewForm.linkTel"></el-input>
							</el-col>
						</el-form-item>
						<el-form-item label="电子邮箱" prop="email" :rules="{ type: 'email', message: '请输入正确的邮箱地址' }">
							<el-col :span="9">
								<el-input placeholder="请输入电子邮箱" v-model="orgViewForm.email"></el-input>
							</el-col>
						</el-form-item>
						<el-form-item label="网站地址">
							<el-col :span="12">
								<el-input placeholder="请输入网站地址" v-model="orgViewForm.webUrl"></el-input>
							</el-col>
						</el-form-item>
						<el-form-item label="生效日期">
							<el-col :span="7">
								<el-date-picker value-format="yyyy-MM-dd" type="date" placeholder="生效日期" v-model="orgViewForm.startDate" style="width: 100%;"
								 :picker-options='startDate'></el-date-picker>
							</el-col>
							<el-col class="line" :span="2" style="text-align: center;">-</el-col>
							<el-col :span="7">
								<el-date-picker value-format="yyyy-MM-dd" type="date" placeholder="失效日期" v-model="orgViewForm.endDate" style="width: 100%;"
								 :picker-options='endDate'></el-date-picker>
							</el-col>
						</el-form-item>
						<el-form-item label="备注">
							<el-col :span="20">
								<el-input placeholder="请输入备注" type="textarea" v-model="orgViewForm.remark"></el-input>
							</el-col>
						</el-form-item>
					</el-form>
					<el-form v-show="!enableEditFlag" :model="orgViewForm" label-width="100px">
						<el-form-item label="机构名称">
							<el-col :span="12">
								<span>{{ orgViewForm.orgName }}</span>
							</el-col>
						</el-form-item>
						<el-form-item label="机构代码">
							<el-col :span="9">
								<span>{{ orgViewForm.orgCode }}</span>
							</el-col>
						</el-form-item>
						<el-form-item label="隶属机构">
							<el-col :span="18">
								<span>{{ orgViewForm.parentOrgName }}</span>
							</el-col>
						</el-form-item>
						<el-form-item label="机构类型">
							<span>{{ orgViewForm.orgTypeName }}</span>
						</el-form-item>
						<el-form-item label="机构等级">
							<span>{{ orgViewForm.orgDegreeName }}</span>
						</el-form-item>
						<el-form-item label="排序">
							<el-col :span="5">
								<span>{{ orgViewForm.sortNo }}</span>
							</el-col>
						</el-form-item>
						<el-form-item label="机构地址">
							<el-col :span="15">
								<span>{{ orgViewForm.address }}</span>
							</el-col>
						</el-form-item>
						<el-form-item label="邮编">
							<el-col :span="8">
								<span>{{ orgViewForm.zipCode }}</span>
							</el-col>
						</el-form-item>
						<el-form-item label="联系人">
							<el-col :span="6">
								<span>{{ orgViewForm.linkMan }}</span>
							</el-col>
						</el-form-item>
						<el-form-item label="联系电话">
							<el-col :span="9">
								<span>{{ orgViewForm.linkTel }}</span>
							</el-col>
						</el-form-item>
						<el-form-item label="电子邮箱">
							<el-col :span="9">
								<span>{{ orgViewForm.email }}</span>
							</el-col>
						</el-form-item>
						<el-form-item label="网站地址">
							<el-col :span="12">
								<span>{{ orgViewForm.webUrl }}</span>
							</el-col>
						</el-form-item>
						<el-form-item label="生效日期">
							<el-col :span="12">
								<span>{{ orgViewForm.startDate?orgViewForm.startDate.substring(0,10):"" }}</span>
								-
								<span>{{ orgViewForm.endDate?orgViewForm.endDate.substring(0,10):"" }}</span>
							</el-col>
						</el-form-item>
						<el-form-item label="备注">
							<span>{{ orgViewForm.remark }}</span>
						</el-form-item>
					</el-form>
				</div>
			</el-card>
			<!-- 弹窗-新增机构 -->
			<el-dialog title="机构信息" :visible.sync="orgDialogVisible" :close-on-click-modal="false" :close-on-press-escape="false"
			 @close="orgDialogCloseEvt">
				<el-form :model="orgNewForm" ref="orgNewForm" label-width="100px">
					<el-form-item label="机构名称" prop="orgName" :rules="[
							{
								required: true,
								message: '机构名称不能为空'
							}
						]">
						<el-col :span="12">
							<el-input placeholder="请输入机构名称" v-model="orgNewForm.orgName"></el-input>
						</el-col>
					</el-form-item>
					<el-form-item label="机构代码" prop="orgCode" :rules="[
							{
								required: true,
								message: '机构代码不能为空'
							}
						]">
						<el-col :span="9">
							<el-input placeholder="请输入机构代码" v-model="orgNewForm.orgCode"></el-input>
						</el-col>
					</el-form-item>
					<el-form-item label="隶属机构" prop="parentOrgName">
						<el-col :span="18">
							<el-input readonly placeholder="请输入隶属机构" v-model="orgNewForm.parentOrgName">
								<!-- 便利的清空按钮-->
								<!-- <i v-show="orgNewForm.parentOrgName" slot="suffix" class="el-input__icon el-icon-circle-close" @click="clearPOrg"
                  style="cursor: pointer;"></i> -->
								<el-button @click="pOrgShowEvt" slot="append" icon="el-icon-search"></el-button>
							</el-input>
						</el-col>
					</el-form-item>
					<el-form-item label="机构类型" prop="orgType">
						<el-radio-group v-model="orgNewForm.orgType">
							<el-radio v-for="item in orgTypeItems" :label="item.id" :key="item.id" border>{{ item.name }}</el-radio>
						</el-radio-group>
					</el-form-item>
					<el-form-item label="机构等级" prop="orgDegree">
						<el-radio-group v-model="orgNewForm.orgDegree">
							<el-radio v-for="item in orgDegreeItems" :label="item.id" :key="item.id" border>{{ item.name }}</el-radio>
						</el-radio-group>
					</el-form-item>
					<el-form-item label="排序" prop="sortNo">
						<el-col :span="5">
							<el-input-number :precision="0" :min="1" v-model="orgNewForm.sortNo"></el-input-number>
						</el-col>
					</el-form-item>
					<el-form-item label="机构地址" prop="address">
						<el-col :span="15">
							<el-input placeholder="请输入机构地址" v-model="orgNewForm.address"></el-input>
						</el-col>
					</el-form-item>
					<el-form-item label="邮编" prop="zipCode">
						<el-col :span="8">
							<el-input placeholder="请输入邮编" v-model="orgNewForm.zipCode"></el-input>
						</el-col>
					</el-form-item>
					<el-form-item label="联系人" prop="linkMan">
						<el-col :span="6">
							<el-input placeholder="请输入联系人" v-model="orgNewForm.linkMan"></el-input>
						</el-col>
					</el-form-item>
					<el-form-item label="联系电话" prop="linkTel">
						<el-col :span="9">
							<el-input placeholder="请输入联系电话" v-model="orgNewForm.linkTel"></el-input>
						</el-col>
					</el-form-item>
					<el-form-item label="电子邮箱" prop="email" :rules="{ type: 'email', message: '请输入正确的邮箱地址' }">
						<el-col :span="9">
							<el-input placeholder="请输入电子邮箱" v-model="orgNewForm.email"></el-input>
						</el-col>
					</el-form-item>
					<el-form-item label="网站地址" prop="webUrl">
						<el-col :span="12">
							<el-input placeholder="请输入网站地址" v-model="orgNewForm.webUrl"></el-input>
						</el-col>
					</el-form-item>
					<el-form-item label="生效日期">
						<el-col :span="11">
							<el-form-item prop="startDate" style="margin-bottom: 0;">
								<el-date-picker value-format="yyyy-MM-dd" type="date" placeholder="生效日期" v-model="orgNewForm.startDate" style="width: 100%;"
								 :picker-options='startDateNew'></el-date-picker>
							</el-form-item>
						</el-col>
						<el-col class="line" :span="2" style="text-align: center;">-</el-col>
						<el-col :span="11">
							<el-form-item prop="endDate" style="margin-bottom: 0;">
								<el-date-picker value-format="yyyy-MM-dd" type="date" placeholder="失效日期" v-model="orgNewForm.endDate" style="width: 100%;"
								 :picker-options='endDateNew'></el-date-picker>
							</el-form-item>
						</el-col>
					</el-form-item>
					<el-form-item label="备注" prop="remark">
						<el-input placeholder="请输入备注" type="textarea" v-model="orgNewForm.remark"></el-input>
					</el-form-item>
				</el-form>
				<div slot="footer" class="dialog-footer">
					<el-button type="primary" @click="orgNewEvt">保存 <i class="el-icon-document-checked el-icon--right"></i></el-button>
					<el-button type="danger" @click="orgDialogVisible = false">关闭 <i class="el-icon-close"></i></el-button>
				</div>
			</el-dialog>

			<!-- 弹窗-选择隶属机构 -->
			<el-dialog custom-class="el-dialog-flex" title="选择隶属机构" :visible.sync="pOrgDialogVisible" :close-on-click-modal="false"
			 :close-on-press-escape="false" @closed="pOrgDialogCloseEvt">
				<el-input style="margin-bottom: 10px;" v-model="pOrgNameSearch" placeholder="请搜索..." suffix-icon="el-icon-search"
				 clearable></el-input>
				<el-tree v-show="pOrgNameSearch" default-expand-all :ref="pOrgNameSearch?'pOrgTree':''" node-key="orgId"
				 show-checkbox :props="orgTreeProps" :check-strictly="true" :check-on-click-node="true" @check-change="pOrgCheckChange"
				 :data="pOrgSearchDatas"></el-tree>
				<el-tree v-show="!pOrgNameSearch" :ref="!pOrgNameSearch?'pOrgTree':''" node-key="orgId" lazy show-checkbox :props="orgTreeProps"
				 :load="loadOrgDatas" :check-strictly="true" :check-on-click-node="true" @check-change="pOrgCheckChange"></el-tree>
				<div slot="footer" class="dialog-footer">
					<el-button type="primary" @click="pOrgSureEvt">确 定 <i class="el-icon-document-checked el-icon--right"></i></el-button>
					<el-button @click="pOrgDialogVisible = false">取 消 <i class="el-icon-refresh-left el-icon--right"></i></el-button>
				</div>
			</el-dialog>
		</el-main>
	</el-container>
</template>
<script>
	import {
		Tree,
		Dialog,
		MessageBox,
		Message
	} from 'element-ui';
	import {
		getOrgSearchRes, //查询机构
		postOrg, //保存机构
		getRootOrg, //获取顶级机构
		getParentOrg, //获取父级机构
		getOrgById, //获取机构详情（根据orgId）
		deleteOrg, //删除机构
		getOrgType, //获取机构类型
		getOrgDegree, //获取机构等级
	} from '@/api/org/organization/organization.js';
	export default {
		components: {
			//注册插件
			'el-tree': Tree, //树形菜单
			'el-dialog': Dialog //弹窗
		},
		watch: {
			/* 机构树搜索数据源处理 */
			orgNameSearch(val) {
				if (val) {
					getOrgSearchRes(val).then(res => {
						let orgDatas = res.data.data;
						this.removeTip() //移除默认提示
						this.deepChild(orgDatas);
						this.orgSearchDatas = orgDatas;
					});
				} else {
					this.treeKey += 1;
				}
			},
			/* 隶属机构树搜索数据源处理 */
			pOrgNameSearch(val) {
				if (val) {
					getOrgSearchRes(val).then(res => {
						let orgDatas = res.data.data;
						this.removeTip() //移除默认提示
						this.deepChild(orgDatas);
						this.pOrgSearchDatas = orgDatas;
					});
				} else {
					this.$refs.pOrgTree.setCheckedKeys([])
					setTimeout(() => {
						this.$refs.pOrgTree.setCheckedKeys([])
					}, 0)

				}
			}
		},
		data() {
			return {
				treeKey: 1,
				treeExpandedKey: [230], //默认展开的树形节点
				orgSearchDatas: [], //搜索返回的树形数据
				orgNameSearch: '', //根据组织名称模糊搜索
				pOrgNameSearch: '', //隶属机构搜索控制
				pOrgSearchDatas: [], //隶属机构数据源
				enableEditFlag: '', //开启form表单编辑，视图切换
				orgNewForm: {
					"address": "",
					"email": "",
					"endDate": "",
					"linkMan": "",
					"linkTel": "",
					"orgCode": "",
					"orgDegree": "",
					"orgDegreeName": "",
					"orgName": "",
					"orgType": "",
					"orgTypeName": "",
					"parentOrgId": "",
					"parentOrgName": "",
					"province": "",
					"provinceName": "",
					"remark": "",
					"sortNo": "",
					"startDate": "",
					"webUrl": "",
					"zipCode": ""
				}, //新增组织数据源
				orgViewForm: {
					"address": "",
					"email": "",
					"endDate": "",
					"linkMan": "",
					"linkTel": "",
					"orgCode": "",
					"orgDegree": "",
					"orgDegreeName": "",
					"orgName": "",
					"orgType": "",
					"orgTypeName": "",
					"parentOrgId": "",
					"parentOrgName": "",
					"province": "",
					"provinceName": "",
					"remark": "",
					"sortNo": "",
					"startDate": "",
					"webUrl": "",
					"zipCode": ""
				}, //修改组织数据源
				orgTypeItems: [], //机构类型（下拉）数据源
				orgDegreeItems: [], //机构等级（下拉）数据源
				//组织树默认配置
				orgTreeProps: {
					label: 'orgName',
					isLeaf: 'isLeaf'
				},
				orgDialogVisible: false, //模态框隐藏
				pOrgDialogVisible: false, //选择隶属机构模态框隐藏
				//生效时间不能大于失效时间
				startDate: {
					disabledDate: time => {
						console.log(this.orgViewForm.endDate);
						if (this.orgViewForm.endDate) {
							return (
								time.getTime() > new Date(this.orgViewForm.endDate).getTime() //不能选择的时间值
							)
						}
					}
				},
				// 失效时间不能早于生效时间
				endDate: {
					disabledDate: time => {
						if (this.orgViewForm.startDate) {
							return (
								time.getTime() < new Date(this.orgViewForm.startDate).getTime() - 8.64e7 // 加- 8.64e7则表示包当天
							)
						}
					}
				},
				startDateNew: {
					disabledDate: time => {
						if (this.orgNewForm.endDate) {
							return (
								time.getTime() > new Date(this.orgNewForm.endDate).getTime() //不能选择的时间值
							)
						}
					}
				},
				// 失效时间不能早于生效时间
				endDateNew: {
					disabledDate: time => {
						if (this.orgNewForm.startDate) {
							return (
								time.getTime() < new Date(this.orgNewForm.startDate).getTime() - 8.64e7 // 加- 8.64e7则表示包当天
							)
						}
					}
				}
			};
		},
		methods: {
			/*移除默认提示 */
			removeTip() {
				let msgs = document.getElementsByClassName("el-message")
				for (let i = 0, len = msgs.length; i < len; i++) {
					msgs[i].style.display = "none";
				}
			},
			/* 递归遍历树形数据源 */
			deepChild(val) {
				let $this = this;
				if (val && Array.isArray(val) && val.length) {
					val.forEach(item => {
						if (typeof item === 'object') {
							item.orgId = item.id;
							item.orgName = item.name;
							$this.deepChild(item.children);
						}
					});
				}
			},
			/* 编辑点击事件 */
			enableEditEvt() {
				this.enableEditFlag = true;
			},
			/* 取消事件 */
			orgEditCancel() {
				this.setOrgViewForm(this.$refs.orgTree.getCurrentNode())
				//this.orgTreeNodeClick()
			},
			/* 开启新增模态框 */
			orgDialogShow() {
				this.orgDialogVisible = true;
				let cData = this.$refs.orgTree.getCurrentNode();
				if (cData) {
					this.orgNewForm.parentOrgId = cData.orgId; //把父节点的组织名称赋给隶属机构
					this.orgNewForm.parentOrgName = cData.orgName; //把父节点的组织名称赋给隶属机构
				}
			},
			/* 递归获取父级节点数据，并设置默认展开 */
			iterParentOrg(pId) {
				if (pId) {
					this.treeExpandedKey.push(pId);
					let pData = this.$refs.orgTree.getNode(pId).data;
					pId = pData.parentOrgId;
					this.iterParentOrg(pId);
				}
			},
			/* 新增时，清空隶属机构*/
			clearPOrg() {
				this.orgNewForm.parentOrgName = "";
				this.orgNewForm.parentOrgId = "";
			},
			/* 过滤无效数据*/
			filterInvalid(dataSource, empty) {
				let validData = {};
				let jsonData = JSON.stringify(dataSource);
				for (let k in dataSource) {
					let data = dataSource[k];
					let condition = data != null && data != undefined && jsonData != "{}" && jsonData != "[]";
					if (empty == "") { //过滤""
						condition = condition && data != "";
					}
					if (condition) {
						validData[k] = data;
					}
				}
				return validData;
			},
			/* 新增组织，保存事件 */
			orgNewEvt() {
				this.$refs.orgNewForm.validate(valid => {
					if (!valid) return false;
					let sendDatas = this.filterInvalid(this.orgNewForm, ""); //过滤表单无效数据
					postOrg(sendDatas).then(res => {
						if (res.data.code != 10000) return;
						Message.success("保存成功！"); //成功提示
						this.orgDialogVisible = false; //关闭窗口
						//this.orgNameSearch = '';
						let nData = JSON.parse(JSON.stringify(this.orgNewForm));
						nData.isLeaf = true; //新增的节点默认为叶子节点
						nData.orgId = res.data.data; //保存组织id
						let pId = nData.parentOrgId; //获取要新增的父节点id
						if (pId) { //已知父节点新增子节点
							let pNode = this.$refs.orgTree.getNode(pId);
							if (pNode.isLeaf) { //如果是叶子节点
								pNode.isLeaf = false;
								pNode.isLeafByUser = false; //设置为非叶子节点
								pNode.loaded = true; //设置为已加载状态
								pNode.expanded = true; //设置父节点展开
							}
							this.$refs.orgTree.append(nData, pId); //手动插入树节点，不刷新树
						} else { //无父节点，视为添加顶级节点，则刷新树
							this.treeKey += 1; //如新增根节点，则刷新树
						}
					});
				});
			},
			/* 修改组织，保存事件 */
			orgEditEvt() {
				let $this = this;
				$this.$refs.orgViewForm.validate(valid => {
					if (!valid) return false;
					let sendDatas = {
						address: this.orgViewForm.address,
						email: this.orgViewForm.email,
						endDate: this.orgViewForm.endDate,
						linkMan: this.orgViewForm.linkMan,
						linkTel: this.orgViewForm.linkTel,
						orgCode: this.orgViewForm.orgCode,
						orgDegree: this.orgViewForm.orgDegree,
						orgId: this.orgViewForm.orgId,
						orgName: this.orgViewForm.orgName,
						orgLevel: this.orgViewForm.orgLevel,
						orgType: this.orgViewForm.orgType,
						parentOrgId: this.orgViewForm.parentOrgId,
						remark: this.orgViewForm.remark,
						sortNo: this.orgViewForm.sortNo,
						startDate: this.orgViewForm.startDate,
						status: this.orgViewForm.status,
						webUrl: this.orgViewForm.webUrl,
						zipCode: this.orgViewForm.zipCode,
					}; //过滤表单无效数据,保留空格
					postOrg(sendDatas).then((res) => {
						if (res.data.code != 10000) return;
						this.enableEditFlag = false; //禁用编辑
						Message.success("修改成功"); //成功提示
						this.orgDialogVisible = false; //关闭窗口
						//$this.loadOrgDatas($this.node, $this.resolve); //刷新树
						//$this.treeKey += 1; //刷新树
						this.orgTreeNodeClick()
					});
				});
			},
			/* 组织树节点，点击事件 */
			setOrgViewForm(data) {
				if (!data) {
					data = this.orgViewForm
				}
				data = JSON.parse(JSON.stringify(data))
				this.$refs.orgViewForm.resetFields();
				this.enableEditFlag = false; //禁用编辑
				this.orgViewForm = data
				//设置隶属机构显示名称
				let cData = this.$refs.orgTree.getCurrentNode();
				let orgId = cData.parentOrgId;
				if (orgId) {
					//如果存在父节点，则获取到父节点的数据
					let pOrgData = this.$refs.orgTree.getNode(orgId).data;
					this.orgViewForm.parentOrgName = pOrgData.orgName; //把父节点的组织名称赋给隶属机构
				}
			},
			orgTreeNodeClick(data) {
				let orgId
				if (data && data.orgId) {
					orgId = data.orgId
				} else {
					orgId = this.$refs.orgTree.getCurrentNode().orgId
				}
				getOrgById(orgId).then(res => {
					this.setOrgViewForm(res.data.data)
				});
			},
			/* 加载组织树 */
			loadOrgDatas(node, resolve) {
				this.node = node;
				this.resolve = resolve;
				if (node.level == 0) {
					let $this = this;
					getRootOrg().then(res => {
						let orgDatas = res.data.data;
						orgDatas.forEach(e => {
							if (e.isLeaf == 'n') {
								e.isLeaf = false;
							} else {
								e.isLeaf = true;
							}
						});
						$this.orgViewForm = JSON.parse(JSON.stringify(orgDatas[0]));
						resolve(orgDatas);
						this.$refs.orgTree.setCurrentKey(orgDatas[0].orgId);
						this.treeExpandedKey = [orgDatas[0].orgId];
					});
				} else {
					getParentOrg(node.data.orgId).then(res => {
							let orgDatas = res.data.data.records;
							orgDatas.forEach(e => {
								if (e.isLeaf == 'n') {
									e.isLeaf = false;
								} else {
									e.isLeaf = true;
								}
							});
							resolve(orgDatas);
						})
						.catch(res => {
							resolve([]);
						});
				}
			},
			/* 删除组织树 */
			orgDelEvent() {
				MessageBox.confirm('此操作将永久删除该机构, 是否继续?').then(() => {
					deleteOrg(this.orgViewForm.orgId).then(res => {
						Message.success(res.data.msg); //成功提示
						//this.treeKey+=1;
						let cData = this.$refs.orgTree.getCurrentNode();
						let orgId = cData.orgId;
						let pOrgId = cData.parentOrgId;
						this.$refs.orgTree.remove(orgId);
						this.$refs.orgTree.setCurrentKey(pOrgId); //删除后默认选中父节点
						this.orgViewForm = JSON.parse(JSON.stringify(this.$refs.orgTree.getCurrentNode())); //更新表单数据
						this.enableEditFlag = false; //关闭编辑视图
					});
				});
			},
			/* 获取机构类型数据源 */
			renderOrgType() {
				getOrgType().then(res => {
					this.orgTypeItems = res.data.data;
				});
			},
			/* 获取机构等级数据源 */
			renderOrgDegree() {
				getOrgDegree().then(res => {
					this.orgDegreeItems = res.data.data;
				});
			},
			/* 新增弹窗，关闭事件 */
			orgDialogCloseEvt() {
				this.$refs.orgNewForm.resetFields(); //清空校验
			},
			pOrgDialogCloseEvt() {
				this.$refs.pOrgTree.setCheckedKeys([])
			},
			/* 选择组织弹窗，开启事件 */
			pOrgShowEvt() {
				this.pOrgDialogVisible = true;
			},
			/* 选择组织弹窗，确定事件 */
			pOrgSureEvt() {

				let cData = this.$refs.pOrgTree.getCurrentNode();
				if (!cData) {
					//如果未选中节点，则视为清空操作
					cData = {
						orgName: '',
						orgId: ''
					};
				}
				if (this.orgDialogVisible) {
					this.orgNewForm.parentOrgName = cData.orgName; //赋显示值
					this.orgNewForm.parentOrgId = cData.orgId; //赋真实值
				} else {
					this.orgViewForm.parentOrgName = cData.orgName; //赋显示值
					this.orgViewForm.parentOrgId = cData.orgId; //赋真实值
				}
				this.pOrgDialogVisible = false; //关闭弹窗
			},
			/* 选择组织弹窗，选中改变事件，用于设置单选模式 */
			pOrgCheckChange(data, checked, indeterminate) {
				if (checked) {
					this.$refs.pOrgTree.setCheckedKeys([data.orgId]);
				}
			}
		},
		created() {
			this.renderOrgType(); //获取机构类型数据源
			this.renderOrgDegree(); //获取机构等级数据源
		}
	};
</script>
<style lang="scss">
	.el-button.el-button-pad {
		padding-left: 8px;
		padding-right: 8px;
	}
</style>
